package com.itgungnir.designpattern.flyweight;

import java.util.HashMap;
import java.util.Map;

public class MyCharFactory {
	private static MyCharFactory instance;

	private Map<Character, MyChar> charMap;

	private MyCharFactory() {
		this.charMap = new HashMap<Character, MyChar>();
	}

	/**
	 * 单例
	 */
	public static MyCharFactory getInstance() {
		if (instance == null) {
			synchronized (MyCharFactory.class) {
				if (instance == null) {
					instance = new MyCharFactory();
				}
			}
		}
		return instance;
	}

	/**
	 * 根据字符的键获取字符对象
	 */
	public MyChar getMyChar(Character character) {
		MyChar c = charMap.get(character);
		if (c == null) {
			c = new MyCharImpl(character);
			charMap.put(character, c);
		}
		return c;
	}

	/**
	 * 获取Map中存储的字符的数量
	 */
	public int getCharCount() {
		return charMap.size();
	}
}